/*
 * @Description: 流程管理相关接口
 * @Author: Rfan
 * @Date: 2023-01-11 10:00:25
 * @LastEditTime: 2023-02-28 11:00:42
 */

import { Key } from 'react';
import { flowRequest, flowRequestV2 } from '@/utils/request';
import { IPageQuery } from '@/interfaces/global';

export interface IStartAuditProps {
  businessKey: string;
  identity: string;
  modelType: string;
}
export interface IProcessDetailParams {
  assignee?: string;
  assigneeType?: number;
  modelType?: string;
}

export interface ICompleteProcessDetailParams {
  comment?: string;
  id?: string;
  params?: Params;
  processInstanceId?: string;
  userId?: string;
}
export interface Params {}

// 启动流程
export function startTask(data: IStartAuditProps) {
  return flowRequest({
    url: `/task/start`,
    method: 'post',
    data
  });
}

// 根据业务ID查询任务详情
export function fetchProcessDetail(businessKey: Key) {
  return flowRequest({
    url: `/task/info/${businessKey}`,
    method: 'get'
  });
}

// 待处理任务
export function fetchProcessDetailByPage(data: IPageQuery<IProcessDetailParams>) {
  return flowRequest({
    url: `/task/untreated/page/`,
    method: 'post',
    data
  });
}

// 完成审批的
export function processDetailComplete(data: IPageQuery<IProcessDetailParams>) {
  return flowRequest({
    url: `/history/myCompleted/`,
    method: 'post',
    data
  });
}

// 完成当前任务节点
export function completeProcessDetail(data: ICompleteProcessDetailParams) {
  return flowRequest({
    url: `/task/complete/`,
    method: 'post',
    data
  });
}

export interface IStartApplyParams {
  applyFormNo: string;
  flowType: string;
  startUserId: number;
}
// // 新申请
// export function startNewApply(data: IStartApplyParams) {
//   return flowRequest({
//     url: `/process/start`,
//     method: 'post',
//     data
//   });
// }

export interface ITaskListResolve {
  applyFormNo: Key;
  durationInMillis: number;
  endTime: string;
  processDefinitionId: string;
  processDefinitionKey: string;
  processDefinitionName: string;
  processDefinitionVersion: number;
  processInstanceId: string;
  startActivityId: string;
  startTime: string;
  startUserId: string;
  startUserName: string;
  task: Task;
  taskState: number;
}

export interface Task {
  applyFormNo: string;
  assignee: string;
  assigneeName: string;
  data: any;
  name: string;
  processInstanceId: string;
  processName: string;
  startTime: string;
  startUserId: string;
  startUserName: string;
  status: string;
  taskId: string;
}

export interface IApplyListParams {
  flowType?: string;
  startUserId?: string;
  startedAfter?: string;
  startedBefore?: string;
  taskState?: number;
}
// // 分页查询我的申请
// export function fetchApplyListByPage(data: IPageQuery<IApplyListParams>) {
//   return flowRequest({
//     url: `/process/my/page`,
//     method: 'post',
//     data
//   });
// }

export interface ITaskProcessResolve {
  endActivityId: string;
  list: ProcessList[];
  startActivityId: string;
  xml: string;
}

export interface ProcessList {
  activityId: string;
  activityName: string;
  activityType: string;
  assignee: string;
  assigneeName: string;
  comment: string;
  durationInSeconds: number;
  endTime: string;
  executionId: string;
  form: any;
  id: string;
  processDefinitionId: string;
  processInstanceId: string;
  startTime: string;
  taskId: string;
}

// // 查询任务详情
// export function fetchTaskDetail(processInstanceId: Key) {
//   return flowRequest({
//     url: `process/info/${processInstanceId}`,
//     method: 'get'
//   });
// }

export interface IApprovalListParams {
  flowType?: string;
  startedAfter?: string;
  startedBefore?: string;
  taskState?: number;
  userId?: string;
}

export interface IApprovalListResolve {
  flowType?: string;
  startedAfter?: string;
  startedBefore?: string;
  taskState?: number;
  userId?: string;
}

// // 分页查询我的申请
// export function fetchApprovalListByPage(data: IPageQuery<IApprovalListParams>) {
//   return flowRequest({
//     url: `/audit/my/page`,
//     method: 'post',
//     data
//   });
// }

export interface IDealTaskParams {
  comment: string;
  processInstanceId: string;
  taskId: string;
  userId: string;
}

// // 处理当前任务节点-同意
// export function dealTaskApprove(data: IDealTaskParams) {
//   return flowRequest({
//     url: `/audit/agree`,
//     method: 'post',
//     data
//   });
// }

// // 处理当前任务节点-同意
// export function dealTaskReject(data: IDealTaskParams) {
//   return flowRequest({
//     url: `/audit/reject`,
//     method: 'post',
//     data
//   });
// }

/**
 * V2接口
 */

// 根据流程ID删除
export function delectByID(data: IDealTaskParams) {
  return flowRequestV2({
    url: `/process/delete`,
    method: 'post',
    data
  });
}
// 流程ID(processInstanceId)查询任务详情
export function fetchTaskDetail(processInstanceId: Key) {
  return flowRequestV2({
    url: `process/info/${processInstanceId}`,
    method: 'get'
  });
}
// 分页查询我的申请
export function fetchApplyListByPage(data: IPageQuery<IApplyListParams>) {
  return flowRequestV2({
    url: `/process/my-page`,
    method: 'post',
    data
  });
}
// 新申请
export function startNewApply(data: IStartApplyParams) {
  return flowRequestV2({
    url: `/process/start`,
    method: 'post',
    data
  });
}

// 分页查询我的审核
export function fetchApprovalListByPage(data: IPageQuery<IApprovalListParams>) {
  return flowRequestV2({
    url: `/audit/my-page`,
    method: 'post',
    data
  });
}
// 处理当前任务节点-同意
export function dealTaskApprove(data: IDealTaskParams) {
  return flowRequestV2({
    url: `/audit/agree`,
    method: 'post',
    data
  });
}
// 处理当前任务节点-拒绝
export function dealTaskReject(data: IDealTaskParams) {
  return flowRequestV2({
    url: `/audit/reject`,
    method: 'post',
    data
  });
}
